package com.hanserwei.mydemo.stream;

import java.util.Comparator;
import java.util.stream.Stream;

public class ReduceTest2 {
    public static void main(String[] args) {

        // 求高手总数
        Hero[] heroes = {
                new Hero("令狐冲", 90),
                new Hero("风清扬", 98),
                new Hero("独孤求败", 100),
                new Hero("方证", 92),
                new Hero("东方不败", 98),
                new Hero("冲虚", 90),
                new Hero("向问天", 88),
                new Hero("任我行", 92),
                new Hero("不戒", 88)
        };

        //求高手总数
        long count = Stream.of(heroes).count();
        System.out.println(count);
        
        //求高手武力最高值
        Stream.of(heroes).max(Comparator.comparingInt(Hero::strength)).ifPresent(System.out::println);
        
        //求高手武力最低值
        Stream.of(heroes).min(Comparator.comparingInt(Hero::strength)).ifPresent(System.out::println);
        
        //求所有英雄的武力值之和
        int sum = Stream.of(heroes).mapToInt(Hero::strength).sum();
        System.out.println(sum);

        //求所有英雄的武力值平均值
        Stream.of(heroes).mapToInt(Hero::strength).average().ifPresent(System.out::println);

    }

    record Hero(String name, int strength) {

    }
}
